package com.dong.mcd.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dong.mcd.dao.DictMapper;
import com.dong.mcd.model.Dict;
import com.dong.mcd.model.DictExample;
import com.dong.mcd.model.DictExample.Criteria;
import com.dong.mcd.param.DictParam;
import com.dong.mcd.service.IDictService;
import com.dong.mcd.util.PageConfig;

@Transactional
@Service("dictService")
public class DictServiceImpl implements IDictService {

	@Autowired
	private DictMapper dictMapper;
	
	public void addDict(Dict dict) {
		dictMapper.insert(dict);
	}

	public void modifyDict(Dict dict) {
		DictExample example = new DictExample();
		example.createCriteria().andCodeEqualTo(dict.getCode());
		dictMapper.updateByExampleSelective(dict, example);
	}

	public List<Dict> query(DictParam param) {
		DictExample example = new DictExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(param.getCode()))
			criteria.andCodeEqualTo(param.getCode());
		if (StringUtils.isNotEmpty(param.getName()))
			criteria.andNameEqualTo(param.getName());
		if (StringUtils.isNotEmpty(param.getType()))
			criteria.andTypeEqualTo(param.getType());
		if (StringUtils.isNotEmpty(param.getValue()))
			criteria.andValueEqualTo(param.getValue());
		if (param.getStatus() != null)
			criteria.andStatusEqualTo(param.getStatus());
		return dictMapper.selectByExample(example);
	}

	public PageConfig queryPage(DictParam param, Integer pageIndex,
			Integer pageSize) {
		DictExample example = new DictExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotEmpty(param.getCode()))
			criteria.andCodeEqualTo(param.getCode());
		if (StringUtils.isNotEmpty(param.getName()))
			criteria.andNameLike("%"+param.getName()+"%");
		if (StringUtils.isNotEmpty(param.getType()))
			criteria.andTypeLike("%"+param.getName()+"%");
		if (param.getStatus() != null)
			criteria.andStatusEqualTo(param.getStatus());
		example.setOrderByClause("type,seq");
		return new PageConfig(dictMapper,example,pageIndex,pageSize);
	}
	public void removeDict(String id) {
			dictMapper.deleteByPrimaryKey(id);
	}

	public Dict getDictById(String id) {
		return dictMapper.selectByPrimaryKey(id);
	}
}
